package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * This class is used to check if the username and password provided in the
 * owner loginbox are valid. The class requires two parameters for the
 * constructor; username and password. If username and password are correct the
 * user will be forwarded to owner.jsp.
 * 
 * @author Olof Ekelund
 */

public class LoginDB {
    private final String username;
    private final String password;
    private String restaurantName;

    LoginDB(String username, String password) {
	this.username = username;
	this.password = password;
    }

    public boolean loginUser() {
	Connection con;
	ResultSet rs;
	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	    Statement stmt = con
		    .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
			    ResultSet.CONCUR_READ_ONLY);

	    String query = "SELECT * FROM olofek.ownerlogin WHERE "
		    + "username = '" + username + "' AND password = '"
		    + password + "';";

	    rs = stmt.executeQuery(query);

	    if (rs.next()) {
		restaurantName = rs.getString("rest_id");
		return true;
	    }
	} catch (Exception e) {
	    System.out.println(e);
	}

	return false;
    }

    public String getRestID() {
	return restaurantName;
    }
}
